package top.hiai.Controller.admin;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import top.hiai.interceptor.AdminInterceptor;
import top.hiai.model.Message;
import top.hiai.model.User;
import top.hiai.service.UserService;
import top.hiai.util.FormatjsonUtil;
import top.hiai.util.Md5Util;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 后台文章分类控制器
*/

public class AdminUserController extends Controller {

	UserService userService = new UserService();
	
	/**
	 * 后台的用户管理
	 */
	@Before(AdminInterceptor.class)
	public void userListTable(){
    	String s_name = get("name");
		//获取用户的ID
        Integer page = getParaToInt("page", 1);
        Integer limit = getParaToInt("limit", 10);
        Page<User> paginate = userService.paginateAdmin(page, limit,s_name);
        renderJson(FormatjsonUtil.layuiPage(paginate));
	}
	/**
	 * 后台设置用户为VIP
	 */
	public void updateVipState(){
		int uid = getInt("uid");
		boolean is_vip = getBoolean("is_vip");
		boolean resuleId = getModel(User.class).setUid(uid).setIs_vip(is_vip).update();
		if(resuleId){
			renderJson("success",true);
		}else{
			renderJson("success",false);
		}
	}
	/**
	 * 后台设置监听用户的状态
	 */
	public void updateUserState(){
		int uid = getInt("uid");
		boolean is_off = getBoolean("is_off");
		boolean resuleId = getModel(User.class).setUid(uid).setIs_off(is_off).update();
		if(resuleId){
			renderJson("success",true);
		}else{
			renderJson("success",false);
		}
	}
	/**
	 * 后台删除用户
	 */
	public void deleteUser(){
		userService.deleteById(getParaToInt());
		redirect("/admin/adminUserManage");
	}
	/**
	 * 后台给用户充值积分
	 */
	public void addPoints(){
		int uid = getInt("uid");
		int points = getInt("points");
		int resultId = userService.setUserAddPoints(uid, points);
		
		if(resultId == 1 ){
			renderJson("success",true);
		}else{
			renderJson("success",false);
		}
	}
	/**
	 * 后台给用户重置密码
	 * 1.重置密码的时候给可以用户发送邮箱
	 * 此处不用给用户发送邮箱，仅做测试，重置密码为123456
	 * 后期可以设置为随机的密码
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public void resetPassword() throws NoSuchAlgorithmException, UnsupportedEncodingException{
		int uid = getInt("uid");
		String password = "123456";	//默认密码
		//String password = StringUtil.genSixRandomNum();	//随机的6位数密码
		
		//对密码进行MD5加密
		password = Md5Util.EncoderPwdByMd5(password);
		Boolean resultId = getModel(User.class).setUid(uid).setPassword(password).update();
		if(resultId){
			renderJson("success",true);
		}else{
			renderJson("success",false);
		}
	}
	/**
	 * 后台给用户发送私信
	 * 1.发送私信用户通知用户的一个信息，后期可以添加发送私信的时候邮箱通知用户
	 * 同时可以添加一键给所有用户发送私信
	 */
	public void addMessage(){
		int uid = getInt("uid");
		String message = "【系统管理员】:"+get("message");
		//发送时间
		Date publish_date = new Date();
		
		Boolean resultId = getModel(Message.class)
				.setContent(message)
				.setPublish_date(publish_date)
				.setUser_id(uid)
				.save();
		if(resultId){
			renderJson("success",true);
		}else{
			renderJson("success",false);
		}
	}
}
